package main

import (
	"bufio"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"os"
	"strings"

	"golang.org/x/crypto/pbkdf2"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("输入hue账号密码: ")
	plain_password, _ := reader.ReadString('\n')
	fmt.Println("密码:" + plain_password)
	// 设置加密参数
	password := []byte(strings.TrimSpace(plain_password))

	reader = bufio.NewReader(os.Stdin)
	fmt.Print("输入salt(12位): ")
	salt_str, _ := reader.ReadString('\n')
	salt_str = strings.TrimSpace(salt_str)
	fmt.Println("salt:" + salt_str)
	if len(salt_str) != 12 {
		fmt.Println("salt位数:" + string(len(salt_str)))
		fmt.Println("salt必须12位")
		os.Exit(1)
	}
	salt := []byte(salt_str)
	iterations := 36000
	keyLen := 32

	// 调用pbkdf2函数进行加密
	key := pbkdf2.Key(password, salt, iterations, keyLen, sha256.New)

	// 转换key为十六进制字符串输出
	fmt.Println("pbkdf2_sha256$36000$" + salt_str + "$" + base64.StdEncoding.EncodeToString(key))
}
